package com.weimob.mcloud.wechatwork.customer.convertor.customer;

import com.weimob.mcloud.wechatwork.core.entity.config.clue.ClueCfg;
import com.weimob.mcloud.wechatwork.core.entity.relation.customer.CustomerProcess;
import com.weimob.mcloud.wechatwork.core.entity.relation.customer.CustomerStage;
import com.weimob.mcloud.wechatwork.core.entity.relation.employee.Department;
import com.weimob.mcloud.wechatwork.core.entity.relation.employee.Employee;
import com.weimob.mcloud.wechatwork.core.enums.clue.PotentialSettingEnum;
import com.weimob.mcloud.wechatwork.core.enums.customer.CustomerProcessAuthBizTypeEnum;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.customer.process.dto.*;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author jiawei.you01
 * @date 2022/9/13 16:22
 * @since 1.0
 */
@Component
public class CustomerProcessConvert {


    public List<CustomerProcessBaseDTO> toCustomerProcessBaseDTOList(List<CustomerProcess> processList) {
        if(CollectionUtils.isEmpty(processList)){
            return new ArrayList<>();
        }
        List<CustomerProcessBaseDTO> result = new ArrayList<>();
        for (CustomerProcess customerProcess : processList) {
            CustomerProcessBaseDTO baseDTO = toCustomerProcessBaseDTO(customerProcess);
            result.add(baseDTO);
        }
        return result;
    }

    private CustomerProcessBaseDTO toCustomerProcessBaseDTO(CustomerProcess customerProcess) {
        CustomerProcessBaseDTO baseDTO = new CustomerProcessBaseDTO();

        baseDTO.setProcessId(customerProcess.getProcessId());
        baseDTO.setProcessName(customerProcess.getProcessName());
        baseDTO.setStatus(customerProcess.getStatusEnum().getCode());
        baseDTO.setProcessAuthList(toProcessAuthDTOList(customerProcess.getDepartments(),customerProcess.getEmployees()));
        baseDTO.setCustomerStage(toCustomerStageBaseDTOList(customerProcess.getCustomerStages()));
        return baseDTO;
    }

    private List<CustomerStageBaseDTO> toCustomerStageBaseDTOList(List<CustomerStage> customerStages) {
        if(CollectionUtils.isEmpty(customerStages)){
            return new ArrayList<>();
        }
        List<CustomerStageBaseDTO> customerStageBaseDTOS = new ArrayList<>();
        for (CustomerStage customerStage : customerStages) {
            customerStageBaseDTOS.add(toCustomerStageBaseDTO(customerStage));
        }
        return customerStageBaseDTOS;
    }

    private CustomerStageBaseDTO toCustomerStageBaseDTO(CustomerStage customerStage) {
        CustomerStageBaseDTO baseDTO = new CustomerStageBaseDTO();
        baseDTO.setStageId(customerStage.getStageId());
        baseDTO.setStageNo(customerStage.getStageNo());
        baseDTO.setStageName(customerStage.getStageName());
        return baseDTO;
    }

    private List<ProcessAuthDTO> toProcessAuthDTOList(List<Department> departments,
                                                      List<Employee> employees) {
        List<ProcessAuthDTO> processAuthDTOS = new ArrayList<>();
        if(!CollectionUtils.isEmpty(departments)) {
            for (Department department : departments) {
                ProcessAuthDTO dto = new ProcessAuthDTO();
                dto.setName(department.getName());
                dto.setBizId(department.getId());
                dto.setBizType(CustomerProcessAuthBizTypeEnum.DEPARTMENT.getCode());
                processAuthDTOS.add(dto);
            }
        }

        if(!CollectionUtils.isEmpty(employees)) {
            for (Employee employee : employees) {
                ProcessAuthDTO dto = new ProcessAuthDTO();
                dto.setName(employee.getName());
                dto.setBizId(employee.getId());
                dto.setBizType(CustomerProcessAuthBizTypeEnum.EMPLOYEE.getCode());
                processAuthDTOS.add(dto);
            }
        }

        return processAuthDTOS;
    }

    public CustomerProcessDetailDTO toCustomerProcessDetailDTO(CustomerProcess customerProcess) {
        CustomerProcessDetailDTO detailDTO = new CustomerProcessDetailDTO();
        detailDTO.setProcessId(customerProcess.getProcessId());
        detailDTO.setProcessName(customerProcess.getProcessName());
        detailDTO.setStatus(customerProcess.getStatusEnum().getCode());
        detailDTO.setProcessAuthList(toProcessAuthDTOList(customerProcess.getDepartments(),customerProcess.getEmployees()));
        detailDTO.setCustomerStage(toCustomerStageDetailDTOList(customerProcess.getCustomerStages()));
        return detailDTO;
    }

    private List<CustomerStageDetailDTO> toCustomerStageDetailDTOList(List<CustomerStage> customerStages) {
        if(CollectionUtils.isEmpty(customerStages)){
            return new ArrayList<>();
        }

        List<CustomerStageDetailDTO> detailDTOS = new ArrayList<>();
        for (CustomerStage customerStage : customerStages) {
            detailDTOS.add(toCustomerStageDetailDTO(customerStage));
        }
        return detailDTOS;
    }

    private CustomerStageDetailDTO toCustomerStageDetailDTO(CustomerStage customerStage) {
        CustomerStageDetailDTO detailDTO = new CustomerStageDetailDTO();
        detailDTO.setStageId(customerStage.getStageId());
        detailDTO.setStageDesc(customerStage.getStageDesc());
        detailDTO.setStageNo(customerStage.getStageNo());
        detailDTO.setStageName(customerStage.getStageName());
        ClueCfg followClueCfg = customerStage
                .getCfgList()
                .stream()
                .filter(i -> i.getPotentialSettingEnum().equals(PotentialSettingEnum.CUSTOMER_PROCESS_FOLLOW))
                .findAny()
                .orElse(null);
        if(followClueCfg != null){
            detailDTO.setFollowCfg(toCustomerStageSettingDTO(followClueCfg));
        }

        ClueCfg roundClueCfg = customerStage
                .getCfgList()
                .stream()
                .filter(i -> i.getPotentialSettingEnum().equals(PotentialSettingEnum.CUSTOMER_PROCESS_CYCLE))
                .findAny()
                .orElse(null);
        if(followClueCfg != null){
            detailDTO.setRoundCfg(toCustomerStageSettingDTO(roundClueCfg));
        }
        return detailDTO;
    }

    private CustomerStageSettingDTO toCustomerStageSettingDTO(ClueCfg clueCfg) {
        CustomerStageSettingDTO settingDTO = new CustomerStageSettingDTO();
        settingDTO.setRuleValue(clueCfg.getRuleValue());
        settingDTO.setRuleOpenFlag(clueCfg.getRuleOpenFlag());
        settingDTO.setRuleType(clueCfg.getPotentialSettingEnum().getCode());
        settingDTO.setRuleExtendInfo(clueCfg.getRuleExtendInfo());
        settingDTO.setId(clueCfg.getId());
        return settingDTO;
    }
}
